<?php

namespace App\Http\Controllers;

use App\Models\Business\Business as BusinessModel;
use App\Models\Business\Address as AddressModel;
use App\Models\Region as RegionModel;

use Illuminate\Http\Request;
// 引入Validator 
use Illuminate\Support\Facades\Validator;


class Address extends Controller
{
    // 地址列表
    public function index()
    {
        $id = request('id', 0);

        $userinfo = BusinessModel::find($id);

        if (!$userinfo) {
            return error('当前用户不存在');
        }

        $result = AddressModel::where('busid', $id)->orderBy('createtime', 'desc')->get();

        return success('', '', $result);
    }

    // 切换默认地址
    public function check()
    {
        $id = request('id', 0);

        $busid = request('busid', 0);

        $userinfo = BusinessModel::find($busid);

        if (!$userinfo) {
            return error('当前用户不存在');
        }

        $addressinfo = AddressModel::find($id);

        if (!$addressinfo) {
            return error('当前收货地址不存在');
        }

        // 首先得找到当前用户下所有的收货地址ID
        $ids = AddressModel::where('busid', $busid)->pluck('id');
        // 批量更新当前拿到的ID的收货地址设置为非默认的
        $res = AddressModel::whereIn('id', $ids)->update(['status' => '0']);
        if ($res === false) {
            return error('更新默认地址失败');
        }

        $data = [
            'id' => $id,
            'status' => '1'
        ];

        $result = AddressModel::where(['id' => $id])->update($data);

        if ($result === false) {
            return error('切换默认地址失败');
        } else {
            return success('切换默认地址成功');
        }
    }

    // 添加地址
    public function add(Request $request)
    {
        $busid = request('busid', 0);

        $params = $request->all();

        $code =  request('code', 0);

        $userinfo = BusinessModel::find($busid);

        if (!$userinfo) {
            return error('当前用户不存在');
        }

        $data = [
            'busid' => $busid,
            'consignee' => $params['consignee'],
            'address' => $params['address'],
            'mobile' => $params['mobile'],
            'status' => $params['status'],
        ];

        $codelist = RegionModel::where('code', $code)->value('parentpath');

        $path = explode(',', $codelist);

        list($province, $city, $district) = $path;

        $data['province'] = $province;
        $data['city'] = $city;
        $data['district'] = $district;

        // 判断是否添加的是默认地址
        if ($params['status'] == 1) {
            // 首先得找到当前用户下所有的收货地址ID
            $ids = AddressModel::where('busid', $busid)->pluck('id');
            // 批量更新当前拿到的ID的收货地址设置为非默认的
            $res = AddressModel::whereIn('id', $ids)->update(['status' => '0']);
            if ($res === false) {
                return error('更新默认地址失败');
            }
        }

        // 验证规则
        $rules = [
            'busid' => 'required',
            'consignee' => 'required',
            'province' => 'required',
            'city' => 'required',
            'district' => 'required',
            'address' => 'required',
            'mobile' => 'required',
        ];

        // 错误提示信息
        $message = [
            'busid.required' => '用户ID必须填写',
            'consignee.required'     => '收件人必须填写',
            'province.required'   => '省份必须填写',
            'city.required'  => '城市必须填写',
            'district.required'     => '区域必须填写',
            'address.required'   => '详细地址必须填写',
            'mobile.required'  => '手机号码必须填写',
        ];

        // make 方法将会生成一个新的验证器实例
        $validator = Validator::make($data, $rules, $message);

        if ($validator->fails()) {
            // errors 获取所有错误信息
            $errors = $validator->errors();
            // first 第一个错误信息;
            return error($errors->first());
        }

        $result = AddressModel::create($data);

        if ($result) {
            return success('添加成功');
        } else {
            return error('添加失败');
        }
    }

    // 地址详情
    public function info()
    {
        $id = request('id', 0);

        $busid = request('busid', 0);

        $userinfo = BusinessModel::find($busid);

        if (!$userinfo) {
            return error('当前用户不存在');
        }

        $addressinfo = AddressModel::find($id);

        if (!$addressinfo) {
            return error('当前收货地址不存在');
        }

        return success('', '', $addressinfo);
    }

    // 修改地址
    public function edit(Request $request)
    {
        $id = request('id', 0);

        $busid = request('busid', 0);

        $params = $request->all();

        $code =  request('code', 0);

        $userinfo = BusinessModel::find($busid);

        if (!$userinfo) {
            return error('当前用户不存在');
        }

        $addressinfo = AddressModel::find($id);

        if (!$addressinfo) {
            return error('当前收货地址不存在');
        }

        $data = [
            'id' => $id,
            'busid' => $busid,
            'consignee' => $params['consignee'],
            'address' => $params['address'],
            'mobile' => $params['mobile'],
            'status' => $params['status'],
        ];

        $codelist = RegionModel::where('code', $code)->value('parentpath');

        $path = explode(',', $codelist);

        list($province, $city, $district) = $path;

        $data['province'] = $province;
        $data['city'] = $city;
        $data['district'] = $district;

        // 判断是否添加的是默认地址
        if ($params['status'] == 1) {
            // 首先得找到当前用户下所有的收货地址ID
            $ids = AddressModel::where('busid', $busid)->pluck('id');
            // 批量更新当前拿到的ID的收货地址设置为非默认的
            $res = AddressModel::whereIn('id', $ids)->update(['status' => '0']);
            if ($res === false) {
                return error('更新默认地址失败');
            }
        }

        $result = AddressModel::where('id', $id)->update($data);
        if ($result === false) {
            return error('修改失败');
        } else {
            return success('修改成功');
        }
    }

    // 删除收货地址
    public function del()
    {
        $id = request('id', 0);

        $busid = request('busid', 0);

        $userinfo = BusinessModel::find($busid);

        if (!$userinfo) {
            return error('当前用户不存在');
        }

        $addressinfo = AddressModel::find($id);

        if (!$addressinfo) {
            return error('当前收货地址不存在');
        }

        $result = AddressModel::where('id', $id)->delete();

        if ($result) {
            return success('删除成功');
        } else {
            return error('删除失败');
        }
    }

    // 获取默认地址
    public function getcheck()
    {
        $busid = request('busid', 0);

        $userinfo =  BusinessModel::find($busid);

        if (!$userinfo) {
            return error('当前用户不存在');
        }

        $addressinfo = AddressModel::where(['busid' => $busid])->get();

        if (!$addressinfo) {
            return error('当前收货地址不存在');
        }

        $data = [];

        foreach ($addressinfo as $item) {
            // 有默认地址
            if ($item['status'] == 1) {
                $data = $item;
            }
        }

        // 没有默认地址就拿第一条数据
        if (empty($data)) {
            $data = $addressinfo[0];
        }

        return success('', '', $data);
    }
}
